/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bsc.war.form;

import bsc.ejb.entity.SRM;
import bsc.ejb.entity.SRMTopic;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Miguel Angel
 */
public class DeleteSRM extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            
            Long idSRM = Long.valueOf(request.getParameter("srm"));
            SRM SRMReal = delegate.findSRM(idSRM);

            List<SRMTopic> srmTopics= delegate.findSRMTopic(SRMReal);

            //Se borran los topics de la RAE
            for (SRMTopic sRMTopic : srmTopics) {
                delegate.deleteSRMTopic(sRMTopic);
            }

            //Se borra la RAE
            delegate.deleteSRM(SRMReal);


            boolean deleted = true;

            
            request.setAttribute("deleted", deleted);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new ModelAndView("deleteSRM");

    }

}
